package com.towexin.application.activiti.controller;

import com.towexin.application.activiti.domain.ProcessForm;
import com.towexin.application.activiti.service.ActivitiesService;
import com.towexin.application.activiti.service.ApproveInfoService;
import com.towexin.application.activiti.service.ProcessFormService;
import com.towexin.common.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * TODO
 *
 * @author Towexin
 * @version 1.0
 * @date 2021/10/18 17:14
 */
@RestController
@RequestMapping("activities")
public class ActivitiesController {

    @Autowired
    private ActivitiesService service;

    @Autowired
    private ProcessFormService processFormService;

    @PostMapping("save/form")
    public ResultData saveOrUpdateProcess(ProcessForm processForm){
        return processFormService.saveOrUpdate(processForm)?ResultData.OK():ResultData.NO();
    }


    /**
     * 获取已部署流程列表
     * @return
     */
    @GetMapping("process/list")
    public ResultData getProcessDefinitionList(){
        return ResultData.OK(service.processDefinitionList());
    }

    /**
     * 导入bpmn流程文件
     * @param file
     * @param name
     * @return
     */
    @PostMapping("process/input")
    public ResultData inputProcessDefinition(MultipartFile file, String name){
        return service.inputProcessDefinition(file, name)?ResultData.OK():ResultData.NO();
    }

    /**
     * 通过xml部署流程
     * @param xml
     * @param name
     * @return
     */
    @PostMapping("process/xml/{bpmnId}")
    public ResultData xmlInputProcessDefinition(@PathVariable String bpmnId, String xml, String name){
        return service.xmlProcessDefinition(bpmnId, xml, name)?ResultData.OK():ResultData.NO();
        //return ResultData.OK();
    }

    /**
     * 删除已部署的流程
     * @param id
     * @return
     */
    @GetMapping("delete/{id}")
    public ResultData deleteProcessDefinition(@PathVariable String id){
        return ResultData.OK(service.deleteProcessDefinition(id));
    }

    @GetMapping("process/form/html/{processDefinitionId}")
    public ResultData getProcessFormHtml(@PathVariable String processDefinitionId){
        return ResultData.OK(processFormService.getById(processDefinitionId).getFormHtml());
    }

    /**
     * 获取流程的xml文本
     * @param id 流程定义id
     * @return
     */
    @GetMapping("process/xml/{id}")
    public ResultData getXML(@PathVariable String id){
        return ResultData.OK(service.getXml(id));
    }

    /**
     * 开始流程
     * @param id 流程定义id
     * @param formJson 表单数据
     * @return
     */
    @PostMapping("start/{id}")
    public ResultData start(@PathVariable String id, String formJson) {
        return service.startActivities(id, formJson)?ResultData.OK():ResultData.NO();
    }

    /**
     * 获取我的待办任务
     * @return
     */
    @GetMapping("my/tasks")
    public ResultData getMyTasks(){
        return ResultData.OK(service.getMyTasks());
    }

    /**
     * 获取我申请的历史
     * @return
     */
    @GetMapping("my/historic")
    public ResultData getMyProcess(){
        return ResultData.OK(service.getMyHistoricProcess());
    }

    /**
     * 删除我的申请
     * @param id 流程实例id
     * @return
     */
    @GetMapping("my/delete/{id}")
    public ResultData deleteProcessInstance(@PathVariable String id){
        return service.deleteProcessInstance(id)?ResultData.OK():ResultData.NO();
    }

    /**
     * 处理业务
     * @param id taskId
     * @param state 是否通过 true|false
     * @return
     */
    @GetMapping("do/{id}/{state}")
    public ResultData trial(@PathVariable String id, @PathVariable boolean state){
        return ResultData.OK(service.doTask(id, state));
    }

    /**
     * 根据流程实例id获取流程进度图
     * @param id 流程实例id
     * @param response
     */
    @GetMapping("img/{id}")
    public void showImg(@PathVariable String id, HttpServletResponse response) {
        service.showImg(id, response);
    }

    /**
     * 根据流程部署id获取流程图
     * @param id 流程部署id
     * @param response
     */
    @GetMapping("process/{id}/{type}")
    public void getProcessImg(HttpServletResponse response, @PathVariable String id, @PathVariable String type){
        service.getFlowChart(response, id, type);
    }


    @GetMapping("tasks/{id}")
    public ResultData getTasks(@PathVariable String id){
        return ResultData.OK(service.getTasks(id));
    }

}
